#include "stdafx.h"
#include "libraries.h"

#include "Application.h"
#include "BitmapFont.h"
#include "Filesystem.h"

using namespace Cvekas;

class FontDemo : public Application
{
public:
	FontDemo(HINSTANCE instance, const std::string& name, const std::string& log_filename)
		:
	Application(instance, name, log_filename)
	{
		Filesystem::init();
	}

	virtual ~FontDemo() {}

	virtual void initVideoDevice()
	{
		settings->insert("video", VideoSettings());

		(*settings)["video"]["ScreenWidth"] = 800;
		(*settings)["video"]["ScreenHeight"] = 600;
		(*settings)["video"]["VSync"] = false;
		(*settings)["video"]["Fullscreen"] = false;

		window = WindowPtr(new Window(instance, name));
		video_device->init(window);
	}

	virtual void init()
	{
		sprite_batch = video_device->createSpriteBatch(main_renderer, 800, 600, 1000);
		impact = BitmapFontPtr(new BitmapFont(sprite_batch, Filesystem::redirect("impact.fnt")));
		consolas = BitmapFontPtr(new BitmapFont(sprite_batch, Filesystem::redirect("consolas.fnt")));
	}

	virtual bool update()
	{
		return true;
	}

	virtual bool render()
	{
		video_device->beginScene();

		impact->draw(Vector2(10.0f, 10.0f), FORMAT("FPS: %d", % Timer::getFPS()), Color(68, 196, 128, 255));
		impact->draw(Vector2(10.0f, 50.0f), "BitmapFont Demo", Color(68, 196, 128, 255));
		impact->drawScaled(Vector2(400.0f, 240.0f), 1.0f + sinf(Timer::getTimeMs() / 500.0f)/4.0f, "Text scaling");
		impact->drawScaled(Vector2(400.0f, 270.0f), 1.0f + sinf(Timer::getTimeMs() / 500.0f + 0.5f)/4.0f, "Text scaling");
		impact->drawScaled(Vector2(400.0f, 300.0f), 1.0f + sinf(Timer::getTimeMs() / 500.0f + 1.0f)/4.0f, "Text scaling");
		impact->drawScaled(Vector2(400.0f, 330.0f), 1.0f + sinf(Timer::getTimeMs() / 500.0f + 1.5f)/4.0f, "Text scaling");

		consolas->draw(Vector2(10.0f, 90.0f), "The quick brown fox jumps over the lazy dog. 123456789", Color(128, 128, 128, 255));

		consolas->drawToRect(RectF(530.0f, 10.0f, 250.0f, 580.0f),
		"His name was Gaal Dornick and he was just a country boy who had never seen Trantor before. "
		"That is, not in real life. He had seen it many times on the hyper-video, and occasionally in "
		"tremendous three-dimensional newscasts covering an Imperial Coronation or the opening of a Galactic Council. "
		"Even though he had lived all his life on the world of Synnax, which circled a star at the edges of the Blue Drift, "
		"he was not cut off from civilization, you see. At that time, no place in the Galaxy was. There were nearly "
		"twenty-five million inhabited planets in the Galaxy then, and not one but owed allegiance to the Empire whose "
		"seat was on Trantor. It was the last halfcentury in which that could be said."); 
		
		sprite_batch->present();
		main_renderer->render();

		video_device->endScene();
		
		main_renderer->endFrame();
		return true;
	}

private:
	SpriteBatchPtr sprite_batch;
	BitmapFontPtr impact, consolas;
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd)
{
	FontDemo demo(hInstance, "BitmapFont Demo", "FontDemo.log");
	try
	{
		demo.run();
	}
	catch (std::exception& e)
	{
		MessageBox(NULL, e.what(), "Fatal error", NULL);
	}
	return 0;
}